using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qVAFARBEN = from m in mtemp.VAFARBEN select m;
       DBMapper.MapVAFARBEN(qVAFARBEN.ToList(), true);
       */
        public List<VAFARBEN> MapVAFARBEN(XElement item2, bool mergedb)
        {
            List<VAFARBEN> result = new List<VAFARBEN>();
            var item4 = from item3 in item2.Elements()
                        select new VAFARBEN
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            VAFMANDANT = (string)item3.Element("VAFMANDANT"),
                            VAFKOLL = (string)item3.Element("VAFKOLL"),
                            VAFART = (string)item3.Element("VAFART"),
                            VAFFARBE = (string)item3.Element("VAFFARBE"),
                            VAFBEZ = (string)item3.Element("VAFBEZ"),
                            VAFFFARBE = (string)item3.Element("VAFFFARBE"),
                            VAFCOLORIT = (string)item3.Element("VAFCOLORIT"),
                            VAFBESCHICH = (string)item3.Element("VAFBESCHICH"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapVAFARBEN(result, mergedb);
          return result;
        }
        ///DBMapper.MapVAFARBEN(mtemp.VAFARBEN.ToList<VAFARBEN>(), true);        
       public void MapVAFARBEN(List<VAFARBEN> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from VAFARBEN row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.VAFARBEN.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.VAFARBEN.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.VAFARBEN.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "VAFARBEN")]
    public class VAFARBEN : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _VAFMANDANT="0";
        [Column]
        public string VAFMANDANT
        {
            get
            {
                return _VAFMANDANT;
            }
            set
            {
                if (value != _VAFMANDANT)
                {
                     _VAFMANDANT = value;
                    NotifyPropertyChanged("VAFMANDANT");
                }
            }
        }
        private string  _VAFKOLL="0";
        [Column]
        public string VAFKOLL
        {
            get
            {
                return _VAFKOLL;
            }
            set
            {
                if (value != _VAFKOLL)
                {
                     _VAFKOLL = value;
                    NotifyPropertyChanged("VAFKOLL");
                }
            }
        }
        private string  _VAFART="0";
        [Column]
        public string VAFART
        {
            get
            {
                return _VAFART;
            }
            set
            {
                if (value != _VAFART)
                {
                     _VAFART = value;
                    NotifyPropertyChanged("VAFART");
                }
            }
        }
        private string  _VAFFARBE="0";
        [Column]
        public string VAFFARBE
        {
            get
            {
                return _VAFFARBE;
            }
            set
            {
                if (value != _VAFFARBE)
                {
                     _VAFFARBE = value;
                    NotifyPropertyChanged("VAFFARBE");
                }
            }
        }
        private string  _VAFBEZ="0";
        [Column]
        public string VAFBEZ
        {
            get
            {
                return _VAFBEZ;
            }
            set
            {
                if (value != _VAFBEZ)
                {
                     _VAFBEZ = value;
                    NotifyPropertyChanged("VAFBEZ");
                }
            }
        }
        private string  _VAFFFARBE="0";
        [Column]
        public string VAFFFARBE
        {
            get
            {
                return _VAFFFARBE;
            }
            set
            {
                if (value != _VAFFFARBE)
                {
                     _VAFFFARBE = value;
                    NotifyPropertyChanged("VAFFFARBE");
                }
            }
        }
        private string  _VAFCOLORIT="0";
        [Column]
        public string VAFCOLORIT
        {
            get
            {
                return _VAFCOLORIT;
            }
            set
            {
                if (value != _VAFCOLORIT)
                {
                     _VAFCOLORIT = value;
                    NotifyPropertyChanged("VAFCOLORIT");
                }
            }
        }
        private string  _VAFBESCHICH="0";
        [Column]
        public string VAFBESCHICH
        {
            get
            {
                return _VAFBESCHICH;
            }
            set
            {
                if (value != _VAFBESCHICH)
                {
                     _VAFBESCHICH = value;
                    NotifyPropertyChanged("VAFBESCHICH");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<VAFARBEN> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\VAFARBEN\\VAFARBEN" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\VAFARBEN" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("VAFARBEN");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (VAFARBEN row in ListInput)
                {
                    XElement f = new XElement("VAFARBEN");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement VAFMANDANT = new XElement("VAFMANDANT");
                         VAFMANDANT.Value = row.VAFMANDANT;
                          f.Add(VAFMANDANT);
                        XElement VAFKOLL = new XElement("VAFKOLL");
                         VAFKOLL.Value = row.VAFKOLL;
                          f.Add(VAFKOLL);
                        XElement VAFART = new XElement("VAFART");
                         VAFART.Value = row.VAFART;
                          f.Add(VAFART);
                        XElement VAFFARBE = new XElement("VAFFARBE");
                         VAFFARBE.Value = row.VAFFARBE;
                          f.Add(VAFFARBE);
                        XElement VAFBEZ = new XElement("VAFBEZ");
                         VAFBEZ.Value = row.VAFBEZ;
                          f.Add(VAFBEZ);
                        XElement VAFFFARBE = new XElement("VAFFFARBE");
                         VAFFFARBE.Value = row.VAFFFARBE;
                          f.Add(VAFFFARBE);
                        XElement VAFCOLORIT = new XElement("VAFCOLORIT");
                         VAFCOLORIT.Value = row.VAFCOLORIT;
                          f.Add(VAFCOLORIT);
                        XElement VAFBESCHICH = new XElement("VAFBESCHICH");
                         VAFBESCHICH.Value = row.VAFBESCHICH;
                          f.Add(VAFBESCHICH);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
